/**
 * 
 */
package app.service.xml;

import static app.constants.CollabVizConstants.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;

import org.apache.commons.lang.StringUtils;
import org.exolab.castor.mapping.Mapping;
import org.exolab.castor.mapping.MappingException;
import org.exolab.castor.xml.MarshalException;
import org.exolab.castor.xml.Marshaller;
import org.exolab.castor.xml.Unmarshaller;
import org.exolab.castor.xml.ValidationException;
import org.xml.sax.InputSource;

import app.constants.CollabVizConstants;
import app.exceptions.CollabVizException;
import app.util.PropertyLoaderUtil;

public class XMLService {

	/**
	 * Represents the mapping between the xml structure and the java object. 
	 */
	static Mapping mappingConfig;

	static{
		mappingConfig = new Mapping();
	
//		try {
////			String mappingFilePath = PropertyLoaderUtil.getProp(XML_MAPPING_FILE);
////			mappingConfig.loadMapping(mappingFilePath);
//		} catch (IOException e) {
//			throw new Error(e);
//		} catch (MappingException e) {
//			throw new Error(e);
//		} catch (CollabVizException e) {
//			throw new Error(e);
//		}
	}

	/**
	 * @param filePath
	 * @return null if file does not exist. 
	 * @throws CollabVizException
	 */
	public static Object unmarshall(String filePath) throws CollabVizException {
		if (filePath == null
				|| org.apache.commons.lang.StringUtils.isEmpty(filePath)) {
			throw new CollabVizException(
					CollabVizConstants.XML_CONFIG_FILE_ERROR);
		}
		try {
			Unmarshaller unmarshal = new Unmarshaller(mappingConfig);
			return unmarshal.unmarshal(new InputSource(new FileReader(
					filePath)));
		} catch (MappingException e) {
			e.printStackTrace();
			throw new CollabVizException(
					CollabVizConstants.FILE_UNMARSHALL_ERROR);
		} catch (MarshalException e) {
			e.printStackTrace();
			throw new CollabVizException(CollabVizConstants.MARSHALL_ERROR);
		} catch (ValidationException e) {
			e.printStackTrace();
			throw new CollabVizException(
					CollabVizConstants.FILE_VALIDATION_ERROR);
		} catch (FileNotFoundException e) {
			return null;
		}
	}

	public static void marshall(String filePath, Object obj) throws CollabVizException {
		if (filePath == null || StringUtils.isEmpty(filePath) || obj == null) {
			throw new CollabVizException(
					CollabVizConstants.FILE_MARSHALL_INPUT_ERROR);
		}
		try {
			Marshaller marshall = new Marshaller(new OutputStreamWriter(
					new FileOutputStream(filePath)));
			marshall.setMapping(mappingConfig);
			marshall.marshal(obj);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			throw new CollabVizException(
					CollabVizConstants.FILE_NOT_FOUND_ERROR);
		} catch (IOException e) {
			e.printStackTrace();
			throw new CollabVizException(CollabVizConstants.FILE_IO_ERROR);
		} catch (MappingException e) {
			e.printStackTrace();
			throw new CollabVizException(CollabVizConstants.MAPPING_FILE_ERROR);
		} catch (MarshalException e) {
			e.printStackTrace();
			throw new CollabVizException(CollabVizConstants.MARSHALL_ERROR);
		} catch (ValidationException e) {
			e.printStackTrace();
			throw new CollabVizException(
					CollabVizConstants.FILE_VALIDATION_ERROR);
		}
	}
}
